package demo.database.H2.D03_元数据;


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;

import javax.sql.DataSource;

import com.titan.core.utils.db.DBUtils;
import com.titan.core.utils.db.DataSourceUtils;
import com.titan.core.utils.db.JdbcUtils;
import com.titan.core.utils.debug.Timer;

import demo.database.H2.utils.H2Utils;

public class D03_获取数据库相关信息 {
    private static void run(final DataSource datasource) throws Exception {
        Connection conn =  datasource.getConnection();  
        ResultSet rs = null;  
        try{    
             DatabaseMetaData dbmd = conn.getMetaData();
             System.out.println("数据库已知的用户: "+ dbmd.getUserName());      
             System.out.println("数据库的系统函数的逗号分隔列表: "+ dbmd.getSystemFunctions());      
             System.out.println("数据库的时间和日期函数的逗号分隔列表: "+ dbmd.getTimeDateFunctions());      
             System.out.println("数据库的字符串函数的逗号分隔列表: "+ dbmd.getStringFunctions());      
             System.out.println("数据库供应商用于 'schema' 的首选术语: "+ dbmd.getSchemaTerm());      
             System.out.println("数据库URL: " + dbmd.getURL());      
             System.out.println("是否允许只读:" + dbmd.isReadOnly());      
             System.out.println("数据库的产品名称:" + dbmd.getDatabaseProductName());      
             System.out.println("数据库的版本:" + dbmd.getDatabaseProductVersion());      
             System.out.println("驱动程序的名称:" + dbmd.getDriverName());      
             System.out.println("驱动程序的版本:" + dbmd.getDriverVersion());
             System.out.println("数据库中使用的表类型");      
             rs = dbmd.getTableTypes();
             while (rs.next()) {      
                 System.out.println(rs.getString("TABLE_TYPE"));      
             }   
        } finally {
            DBUtils.close(rs);
            DBUtils.close(conn);
        }
    }

    
    public static void main(String[] args) {
        Timer timer = Timer.$();
        DataSource datasource = H2Utils.getDataSource();
        try {
            JdbcUtils.setDataSource(datasource);
            run(datasource);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DataSourceUtils.close(datasource);
            timer.print();
        }
    }
}
